﻿package framework.core;


public class PageData 
{
public PageData() 
{
	super();
}
	
	//从表数据得到PageData
	public static PageData fromTable(Table t,Class c) throws Exception
	{
		PageData cls = null;
		try 
	    {
	        Class[] cs = {};
	        Object[] os = {};
	        cls = (PageData) c.getConstructor(cs).newInstance(os);
	    } 
	    catch (Exception e) 
	    {
	    	Logger.throwException("PageData.fromTable()","实例化对象错误:className="+c.getName());
	    }
	       
	    java.lang.reflect.Method[] ms = c.getMethods();
	    Class[] gcs = {};
	    Object[] gos = {};
	    Object   gr = null;
		Class ct = t.getClass();
	    for (int i = 0; i < ms.length; i++) 
	    {
	        try 
	        {
	            java.lang.reflect.Method m = ms[i];
	            String name = m.getName();
	            String value = "";
	            if (name.indexOf("set") == 0) 
	            {
	                name = name.substring(3);
	                
	                try
	                {
	         			java.lang.reflect.Method mget = ct.getMethod("get"+name,gcs);
	         			gr = mget.invoke(t,gos);     
	         			String strV = "";
	         			if(gr instanceof Integer)
         				{
         					Integer iVal = (Integer)gr ;
         					strV = Value.view(iVal.intValue());
         				}
         				else if(gr instanceof Double)
         				{
         					Double iVal = (Double)gr ;
         					strV = Value.view(iVal.doubleValue());
         				}
         				else if(gr instanceof String)
         				{
         					String iVal = (String)gr ;
         					strV = Value.view(iVal);
         				}
	         			if (m.getParameterTypes()[0].equals(String.class)) 
	                	{
	                    	Object[] os = { strV };
		                    m.invoke(cls, os);
	    	            }  
	        	    }
	                catch(Exception e1)
	                {
	                	Logger.log("PageData.fromTable()",e1.getMessage());
	                }
	            }
	        } 
	        catch (Exception e) 
	        {
	            Logger.throwException("PageData.fromTable()",e.getMessage());
	        }
	    }
	    return cls;
	}
	
	//从页面提交的数据得到PageData
	public static PageData fromForm(SrvContext form,Class c) throws Exception 
	{
	    PageData data = null ;
	    try 
	    {
	        Class[] cs = {};
	        Object[] os = {};
	        data = (PageData) c.getConstructor(cs).newInstance(os);
	    } 
	    catch (Exception e) 
	    {
	        Logger.throwException("PageData.fromForm","实例化对象错误:className="+c.getName());
	    }
	
	    java.lang.reflect.Method[] ms = data.getClass().getMethods();
	    for (int i = 0; i < ms.length; i++) 
	    {
	        try 
	        {
	            java.lang.reflect.Method m = ms[i];
	            String name = m.getName();
	            String value = "";
	            if (name.indexOf("set") == 0) 
	            {
	                name = name.substring(3);
	                if (name.length() > 0) 
	                {
	                    value = form.getString(name);
	                    if (m.getParameterTypes()[0].equals(String.class)) 
	                    {
	                    	if (value == null) continue;
	                        Object[] os = { value };
	                        m.invoke(data, os);
	                    }
	                }
	            }
	        } 
	        catch (Exception e) 
	        {
	            Logger.throwException("PageData.fromForm()",e.getMessage());
	        }
	    }
	    return data;
	}
}
